﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using KqAutoDesign.Base;

namespace KqAutoDesign.Models
{
    public class WorkFlowBase : NotifyBase, IClone<WorkFlowBase>, IGetGlobalParams
    {
        public WorkFlowBase() 
        {
            WorkFlowState = WorkFlowStateEnum.NotStarted;
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public ObservableCollection<ModuleBase> Modules { get; set; }
        private WorkFlowStateEnum _wfs;
        public WorkFlowStateEnum WorkFlowState
        { 
            get{ return _wfs; }
            set
            {
                if (_wfs != value)
                {
                    _wfs = value;
                    OnPropertyChanged(nameof(WorkFlowState));
                    //CommandManager.InvalidateRequerySuggested();
                    Application.Current.Dispatcher.Invoke(() => CommandManager.InvalidateRequerySuggested());
                }
            }
        }
        public ModuleBase CurrentModule { get; set; }

        public WorkFlowBase Clone()
        {
            var cloned = MemberwiseClone() as WorkFlowBase;
            if (Modules != null)
                cloned.Modules = new ObservableCollection<ModuleBase>(Modules);
            else
                cloned.Modules = new ObservableCollection<ModuleBase>();
            cloned.CurrentModule = CurrentModule?.Clone();
            return cloned;
        }

        public List<GroupScriptParam> GetGlobalParams()
        {
            List<GroupScriptParam> pms = new List<GroupScriptParam>();
            foreach (var module in Modules)
            {
                var gpms = module.GetGlobalParams();
                foreach (var item in gpms)
                {
                    item.ScriptParam.Name = Name + "_" + item.ScriptParam.Name;
                    item.ScriptParam.Description = Name + "_" +item.ScriptParam.Description;
                }
                pms.AddRange(gpms);
            }
            return pms;
        }

        private string _stateDes;
        public string StateDescription
        {
            get { return _stateDes; }
            set
            {
                if (_stateDes != value)
                {
                    _stateDes = value;
                    OnPropertyChanged(nameof(StateDescription));
                }
            }
        }
        public void Run()
        {
            StateDescription = string.Format("任务{0}开始执行", Name);
            WorkFlowState = WorkFlowStateEnum.Running;
            foreach (var module in Modules)
            {

                module.MakeScriptFiles();
            }
            foreach (var module in Modules)
            {
                StateDescription = string.Format("任务{0},模块{1}开始执行", Name,module.Name);
                CurrentModule = module;
                module.Run();
            }
            WorkFlowState = WorkFlowStateEnum.finished;
        }

        /// <summary>
        /// 设置模块脚本所在工作目录
        /// </summary>
        /// <param name="workDir">工作目录，末尾必须包含斜杠</param>
        public void SetWorkDir(string workDir)
        {
            if(Modules!= null)
            {
                foreach (var md in Modules)
                {
                    md.BatchScript.WorkDir = workDir;
                    foreach (var fs in md.FunctionScripts)
                    {
                        fs.WorkDir = workDir;
                    }
                }
            }
        }
    }

    public enum WorkFlowStateEnum
    {
        [Description("无计划")]
        NoPlan,
        [Description("待开始")]
        NotStarted,
        [Description("运行中")]
        Running,
        [Description("暂停中")]
        Suspended,
        [Description("已结束")]
        finished
    }

    public enum ProjectStateEnum
    {
        [Description("空队列")]
        EmptyQueue,
        [Description("待开始")]
        NotStarted,
        [Description("运行中")]
        Running,
        [Description("暂停中")]
        Suspended,
        [Description("已结束")]
        finished
    }

    public enum ActionMarkEnum
    {
        Stop,//停止
        Suspend,//暂停
        Null//无操作
    }
}
